package com.yami.shop.groupbuy.comment.listener;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yami.shop.bean.event.ProcessGroupProdPriceEvent;
import com.yami.shop.bean.model.Product;
import com.yami.shop.groupbuy.comment.dao.GroupProdMapper;
import com.yami.shop.groupbuy.comment.model.GroupProd;
import com.yami.shop.groupbuy.comment.service.GroupSkuService;
import lombok.AllArgsConstructor;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 处理活动商品价格的事件
 * @author lhd
 */
@Component("processGroupProdPriceListener")
@AllArgsConstructor
public class ProcessGroupProdPriceListener {

    private final GroupProdMapper prodMapper;

    @EventListener(ProcessGroupProdPriceEvent.class)
    public void processSeckillProdPrice(ProcessGroupProdPriceEvent event) {
        List<Product> products = event.getProducts();
        List<Long> prodIds = new ArrayList<>();
        products.forEach(product -> prodIds.add(product.getProdId()));
        List<Product> activityProds = prodMapper.listByProdIds(prodIds);
        Map<Long, Double> priceMap = activityProds.stream().collect(Collectors.toMap(Product::getProdId, Product::getPrice));
        for (Product product : products) {
            Double price = priceMap.get(product.getProdId());
            product.setPrice(price);
        }
    }

}
